package com.dd.census.service.impl;

import com.dd.census.entity.Staff1;
import com.dd.census.mapper.Staff1Mapper;
import com.dd.census.service.DashboardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DashboardServiceImpl implements DashboardService {

    SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd" );

    @Autowired
    Staff1Mapper staff1Mapper;

    @Override
    public Map<String, Object> selectDashboar(String userId) throws ParseException{
        Map<String, Object> map = new HashMap<>();
        //本月入场人数
        Staff1 staff1 = new Staff1();
        staff1.setWorktime(sdf.format(new Date()));
        staff1.setState(1); // 在职
        List<Staff1> staff1s = staff1Mapper.selectListbyMother(staff1);
        map.put("nowMonther",staff1s!=null ?staff1s.size():0);
        // 半年 每个月入职人数
        Calendar now = Calendar.getInstance();
        now.add(GregorianCalendar. MONTH, -5);
        Staff1 staff2 = new Staff1();
        staff2.setWorktimestart(now.get(Calendar.YEAR) + "-" + now.get(Calendar.MONTH)+ "-" +1);
        staff2.setWorktimeend(sdf.format(new Date()));
        List<Staff1> staff2s = staff1Mapper.selectGroupByMother(staff2);
        List a = this.getMonth(now.get(Calendar.YEAR) + "-" + now.get(Calendar.MONTH)+ "-" +1,sdf.format(new Date()));
        Map<String, Object> map1 = new LinkedHashMap <>();
        int count1 = 0;
        for(int i = 0;i<a.size();i++){
            for(Staff1 s1: staff2s){
                if(s1.getWorktime().equals(a.get(i))){
                    count1 = s1.getCountMonth();
                    break;
                }
            }
            map1.put(a.get(i).toString(),count1);
        }
        map.put("halfYear",map1);
        // 一年年 每个月入职人数
        Calendar now2 = Calendar.getInstance();
        now2.add(GregorianCalendar. MONTH, -11);
        Staff1 staff3 = new Staff1();
        staff3.setWorktimestart(now.get(Calendar.YEAR) + "-" + now.get(Calendar.MONTH)+ "-" +1);
        staff3.setWorktimeend(sdf.format(new Date()));
        List<Staff1> staff3s = staff1Mapper.selectGroupByMother(staff3);
        List a2= this.getMonth(now2.get(Calendar.YEAR) + "-" + now2.get(Calendar.MONTH)+ "-" +1,sdf.format(new Date()));
        Map<String, Object> map2 = new LinkedHashMap <>();
        int count = 0;
        for(int i = 0;i< a2.size();i++){
            for(Staff1 s1: staff3s){
                if(s1.getWorktime().equals(a2.get(i))){
                    count = s1.getCountMonth();
                    break;
                }
            }
            map2.put(a2.get(i).toString(),count);
        }
        map.put("oneYear",map2);
        return map;
    }

    private static List getMonth(String startDate, String endDate) throws ParseException {
        List list = new ArrayList();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(sdf.parse(startDate));
        c2.setTime(sdf.parse(endDate));
        int  year = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
        int month = c2.get(Calendar.MONTH)+year*12 - c1.get(Calendar.MONTH);
        for(int i = 1;i<=month;i++){
            c1.setTime(sdf.parse(startDate));
            c1.add(c1.MONTH, i);
            list.add(sdf.format(c1.getTime()));
        }
        return list;
    }
}